<?php
class ImagesController extends CController {
	public $filename;
	public function actionManage() {
		$smarty = Yii::app ()->smarty;
		$smarty->display ( "images/index.html" );
	}
	public function actionContent() {
		$smarty = Yii::app ()->smarty;
		$upload = new Upload ( );
		$fileinfo = $upload->getFiles ();
		$dirList=Dir::model()->getOptionType();
		$smarty->assign ( "fileinfo", $fileinfo );
		$smarty->assign ( "dirList", $dirList);
		$smarty->display ( "images/content.html" );
	}
	public function actionShow() {
		if(Yii::app()->request->isPostRequest){
        if(isset($_POST['ids'])){
		$ids=$_POST['ids'];
        Dir::model()->delDir($ids);
        }
		}
		$smarty = Yii::app ()->smarty;
		$upload = new Upload ( );
		$fileinfo = $upload->getFiles ();
		$dirList=Dir::model()->getAllList();
		$smarty->assign ( "fileinfo", $fileinfo );
		$smarty->assign ( "dirList", $dirList);
		$smarty->display ( "images/show.html" );
	}
	public function actionUpload() {
		$msg = "";
		$filename = "";
		$dirName = Yii::app ()->params ['upload'];
		$upload = new Upload ( );
		if (isset ( $_POST ['id'] )) {
			$dirid=$_POST['dirtype'];
			$filename = $_FILES [$_POST ['id']] ['name'];
			$filetype = pathinfo($_FILES [$_POST ['id']] ['name']);
			$filetype=$filetype['extension'];
			$filesize = $_FILES [$_POST ['id']] ['size'];
			$filesize=round($filesize/1024);
			$this->filename = $filename;
			$tem_name = $_FILES [$_POST ['id']] ['tmp_name'];
			$upload->fileUpload ( $dirName, $filename, $tem_name,$dirid,$filesize,$filetype );
		}
		
		$fileinfo = $upload->find(array("order"=>"id desc","limit"=>1));
		    
		echo "<tr><td>" . $fileinfo->originalname. "</td><td>".$fileinfo->filesize."K</td><td>完成</td><td>删除</td></tr>";
	
	}
	public function actionCreatedir(){
	      if(!empty($_POST['dirname'])){
		  $model=new Dir();
		  $model->name=addslashes($_POST['dirname']);
		  if($model->save()){
		  echo "1";
		  }
		  }else {
		  echo "文件夹名称不能为空！";
		  }
	   }		
	public function actionDeldir(){
	      if(!empty($_POST['dirid'])){
		  $id=addslashes($_POST['dirid']);	
	      $ret=Dir::model()->delDir($id);
          if($ret){
          echo "1";
          } 
		  }		
	    }
	
	public function actionDelfile(){
	      if(!empty($_POST['fileid'])){
		  $id=addslashes($_POST['fileid']);	
		  $path="uploads/".$_POST['path'];
		  $ret=Upload::model()->delFile($id,$path);
          if($ret){
          echo "1";
          } 
	      }		
	    }  
	
	public function actionList(){
		    $smarty = Yii::app ()->smarty;
		    if(isset($_GET['id'])){
            $dirid=$_GET['id'];
			$ret=Upload::model()->getFileListAll($dirid);
			$smarty->assign ( "ret", $ret);
		    $smarty->display ( "images/filelist.html" );
		    }
	     }
	
}
?>